//
//  MyAdressViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/28.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MyAdressViewModel.h"
#import "NOAdressView.h"
#import "MyAdressListTableViewCell.h"

@interface MyAdressViewModel ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *selfTableView;
    UIView * self_View;
    GLGWButton * adressAddbut;
    //
    CGFloat tableViewNowHeight;
}
@property(nonatomic,strong)NSMutableArray * dataSource;
@end

@implementation MyAdressViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self_View = superView;
        self.dataSource = [NSMutableArray array];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(94);
        tableView.tableHeaderView = [self createHeaderView];
        [superView addSubview:tableView];
        selfTableView = tableView;
        tableViewNowHeight = tableView.height;
        [self requestAdressData];
    }
    return self;
}
-(UIView *)createHeaderView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}

-(void)noDataShow{
    if (self.dataSource.count ==0) {
        NOAdressView * sssView = [[NOAdressView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(98), KScreenWidth, kHeightProportion_In_iPhoneTriploid(205))];
        [[sssView.addBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(adressAddAction)]) {
                [self.delegate adressAddAction];
            }
        }];
        [self showNoDataImageView:sssView];
        if (adressAddbut) {
            [adressAddbut removeFromSuperview];
        }
    }else{
        [self removeNoDataImage];
        selfTableView.height = tableViewNowHeight- kHeightProportion_In_iPhoneTriploid(44);
        adressAddbut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(selfTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) titleText:@"再加一条" titleLableFont:SYSTEMFONT(14) titleLableColor:[UIColor whiteColor] inSuperView:self_View];
        adressAddbut.image_Name = @"adress_gord";
        [adressAddbut setImagePositionWithType:SSImagePositionTypeLeft spacing:kWidthProportion_In_iPhoneTriploid(10)];
        adressAddbut.backgroundColor = kColor_Rule_Yellow;
        [[adressAddbut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(adressAddAction)]) {
                [self.delegate adressAddAction];
            }
        }];
    }
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAdressListTableViewCell * cell = [MyAdressListTableViewCell cellWithTableView:tableView];
    cell.Block_EditAction = ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(editAdressInforModel:)]) {
            [self.delegate editAdressInforModel:self.dataSource[indexPath.row]];
        }
    };
    cell.adressInforModel = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark --- 数据请求
-(void)requestAdressData{
    @weakify(self)
    _recommendCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        RACSignal *requstSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSDictionary * adreeDic = @{@"userId":koString(self.userId),
                                        @"pageNo":@([input integerValue]),
                                        @"pageSize":@10};
            if ([input integerValue] == 1) {
                [self.dataSource removeAllObjects];
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kGetwarehouseAddress] lodingView:nil lodingTextString:@"" isLoding:YES parameters:adreeDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [self noDataShow];
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [requstSignal map:^id(NSDictionary *value) {
            MyAdressModel * my_adress = [MyAdressModel yy_modelWithJSON:value];
            if (my_adress.code == 0) {
                [self.dataSource addObjectsFromArray:my_adress.data.infos];
                [self noDataShow];
                [self->selfTableView reloadData];
            }
            return my_adress;
        }];
    }];
}

@end
